import {
  g3x_defineClassProperties,
} from "@g3x/utils";

export default function(Ctor) {
  g3x_defineClassProperties(Ctor, [{
    key: '_updateCurrentPositionAndRotation',
    value: function _updateCurrentPositionAndRotation() {
      var d = this.currentD;
      this._ds = (0, _interpolate.computDs)(this.positions, this.rotations, this.loop, this._ds, this
        ._loopPositions, this._loopRotations);
      var positions = this.loop ? this._loopPositions : this.positions;
      var rotations = this.loop ? this._loopRotations : this.rotations;
      (0, _interpolate.interpolatePosition)(d, this._ds, positions, this._currentPosition);

      if (!this.alwaysAlongThePath) {
        (0, _interpolate.interpolateRotation)(d, this._ds, rotations, this._currentRotation);
      } else {
        computeRotationAlongThePath(d, this._currentPosition, this._ds, this.length, positions, this
          ._currentRotation);
      }

      // if (this.cameraAttached) {
      //     this.currentCameraApply();
      // }
    }

    /**
     * 同时给所有关键点增加高度
     * @param {number} height 高度，单位米
     */

  }, {
    key: 'addHeight',
    value: function addHeight(height) {
      this.positions.forEach(function(e) {
        return e[2] += height;
      });
    }

    /**
     * 将所有关键点的方向指向target所在位置
     * @param {number[]} target 用来表示观察位置，包含三个元素的数组，分别表示经度、纬度、高度
     * @param {number[]} includeIndex 索引数组，如果有值，则指只处理includeIndex有的索引，如果无值，则全部处理
     */

  }, {
    key: 'targetTo',
    value: function targetTo(target, includeIndex) {
      var _this3 = this;

      if (this.positions.length === this.rotations.length) {
        this.positions.forEach(function(e, i) {
          if (!includeIndex || ~includeIndex.indexOf(i)) {
            _utils2.default.Math.hpr(e, target, _this3.rotations[i]);
            _this3.rotations[i][0] += Cesium.Math.PI_OVER_TWO;
          }
        });
      }
    }

    /**
     * 自动延沿路径计算关键点的姿态<br/>
     * 注意：如果不设置参数，从修改所有关键点的姿态。<br/>
     * includeIndex是一个数组，囊括所有需要计算的关键点的索引。
     * @param {number[] | undefined} includeIndex 索引数组，如果有值，则指只处理includeIndex有的索引，如果无值，则全部处理
     */

  }, {
    key: 'directionAlongThePath',
    value: function directionAlongThePath(includeIndex) {
      // var l = this.positions.length;
      // for (let i=0; i<l-1; ++i) {
      //     if (!includeIndex || ~includeIndex.indexOf(i)) {
      //         Tool.Math.hpr(this.positions[i], this.positions[i+1], this.rotations[i]);
      //         this.rotations[i][0] += Cesium.Math.PI_OVER_TWO;
      //     }
      // }

      // // 特殊处理最后一个索引
      // if (!includeIndex || !includeIndex.indexOf(l-1)) {
      //     if (this.loop) { // 如果是循环的话，求取和首点的方向
      //         Tool.Math.hpr(this.positions[l-1], this.positions[0], this.rotations[l-1]);
      //     } else {
      //         // 如果不是循环的，以最后两点连线的方向来定。
      //         Tool.Math.hpr(this.positions[l-2], this.positions[l-1], this.rotations[l-1]);
      //     }
      //     this.rotations[l-1][0] += Cesium.Math.PI_OVER_TWO;
      //     computeRotationAlongThePath(this.ds[l-1], this.positions[l-1], this._ds, this.length, this.positions, this.rotations[l-1]);
      // }

      var l = this.positions.length;
      for (var i = 0; i < l; ++i) {
        if (!includeIndex || ~includeIndex.indexOf(i)) {
          var ii = i + (this.loop ? 1 : 0);
          var positions = this.loop ? this._loopPositions : this.positions;
          computeRotationAlongThePath(this._ds[ii], positions[ii], this._ds, this.length, positions,
            this.rotations[i]);
        }
      }
    }

    /**
     * 根据索引值为first和last的关键点的姿态，对first和last之间的关键点自动进行插值处理。<br/>
     * 这样操作，可以做到自动进行相机姿态的平滑。<br/>
     * 如果参数不写，则自动根据首末关键点的姿态插值中间所有关键点的姿态。<br/>
     * 注意：first务必小于last
     * @param {number | undefined} first 第一个索引
     * @param {number | undefined} last 最后一个索引
     */

  }, {
    key: 'directionLerpBetween',
    value: function directionLerpBetween() {
      var first = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
      var last = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : -1;

      if (last === -1) {
        last = this.positions.length - 1;
      }

      if (last < first) {
        console.warn('directionLerpBetween 参数错误！');
        return;
      }

      this.directionLerpBetween_scratchDs = [];
      (0, _interpolate.computDs)(this.positions, this.rotations, false, this
        .directionLerpBetween_scratchDs);

      var ds = [this._ds[first], this._ds[last]];
      var rots = [this.rotations[first], this.rotations[last]];

      for (var i = first + 1; i < last; ++i) {
        (0, _interpolate.interpolateRotation)(this._ds[i], ds, rots, this.rotations[i]);
      }
    }
  }, {
    key: 'flyTo',
    value: function flyTo() {
      this._polyline.flyTo();
    }
  }, {
    key: 'currentCameraApply',

    /**
     * 应用当前相机姿态<br/>
     * 将该路径的currentPosition和currentRotation设置为相机的position和rotation。
     */
    value: function currentCameraApply() {
      var _Cesium$Cartesian;

      flyCartesian = flyCartesian || new Cesium.Cartesian3();
      var scene = this._earth.czm.scene;

      (_Cesium$Cartesian = Cesium.Cartesian3).fromRadians.apply(_Cesium$Cartesian, (0,
        _toConsumableArray3.default)(this.currentPosition).concat([undefined, flyCartesian]));
      var camera = scene.camera;
      camera.setView({
        destination: flyCartesian,
        orientation: {
          heading: this.currentRotation[0],
          pitch: this.currentRotation[1],
          roll: this.currentRotation[2]
        }
      });
    }
  }, {
    key: '_play',
    value: function _play() {
      var _this4 = this;

      var scene = this._earth.czm.scene;

      // 如果位于末端，则从头开始
      if (this.currentSpeed > 0 && this.currentD === this.length) {
        this.currentD = 0;
      }

      // 如果倒速播放，则从尾开始
      if (this.currentSpeed < 0 && this.currentD === 0) {
        this.currentD = this.length;
      }

      var lastTime = Date.now();
      this._pathPlayingListener = scene.postUpdate.addEventListener(function(scene, time) {
        var currentTime = Date.now();
        var diffTime = currentTime - lastTime;
        lastTime = currentTime;

        if (_this4.currentSpeed > 0) {
          // currentSpeed>0时，往前走
          var currentD = _this4.currentD + diffTime * 0.001 * _this4.currentSpeed;
          if (_this4.loopPlay) {
            if (currentD > _this4.length) {
              _this4.currentD = 0;
            } else {
              _this4.currentD = currentD;
            }
          } else {
            if (currentD > _this4.length) {
              _this4.currentD = _this4.length;
              _this4._pathPlayingListener = _this4._pathPlayingListener && _this4
                ._pathPlayingListener();
            } else {
              _this4.currentD = currentD;
            }
          }
        } else {
          // 考虑一下currentSpeed<0时的走法
          var _currentD = _this4.currentD + diffTime * 0.001 * _this4.currentSpeed;
          if (_this4.loopPlay) {
            // 如果循环播放的话
            if (_currentD < 0) {
              _this4.currentD = _this4.length;
            } else {
              _this4.currentD = _currentD;
            }
          } else {
            if (_currentD < 0) {
              // 播放到0就终止操作
              _this4.currentD = 0;
              _this4._pathPlayingListener = _this4._pathPlayingListener && _this4
                ._pathPlayingListener();
            } else {
              _this4.currentD = _currentD;
            }
          }
        }
      });

      // scene.camera._currentFlight = {
      //     cancelTween: () => {
      //         updateCameraListener();
      //     }
      // }
    }
  }, {
    key: 'destroy',
    value: function destroy() {
      return (0, _get3.default)(Path.prototype.__proto__ || (0, _getPrototypeOf2.default)(Path
        .prototype), 'destroy', this).call(this);
    }
  }, {
    key: 'isCameraLike',
    get: function get() {
      return true;
    }
  }]);
}
